<?php

namespace App\Winged\Tenants ;


use Illuminate\Foundation\Application;

abstract class AbstractTenant{

    public $host ;

    public $app ;

    protected $admin_display_name ;

    protected $admin_display_short_name ;

    protected $repository;

    protected $logo ;

    protected $logo_mini ;

    protected $logo_large ;

    protected $admin_background;

    protected $style;

    protected $admin_title ;

    protected $tenant_uuid ;

    protected $services;

    protected $admin_default_url;

    protected $barcodes;

    protected $config ;

    protected $isCommonDomain = false ;

    protected $ios_push_appid ;

    protected $ios_push_appkey;

    protected $ios_push_secret;

    protected $android_push_appid ;

    protected $android_push_appkey;

    protected $android_push_secret;

    protected $department_domain;

    public function __construct(Application $app)
    {
        $this->app = $app ;
    }

    public function init()
    {
        $tenantName = $this->getTenantNameByHost() ;

        $this->config = $this->getConfig( $tenantName );

        $this->setProtype($this->config);

        $this->overrideConfiguration();

    }

    abstract public function getConfig( $tenantName ) : array  ;

    abstract public function setProtype($config) ;

    public function overrideConfiguration()
    {
        config(['admin.name' => $this->getDisplayName()]);
        config(['admin.logo' => $this->getLogo()]);
        config(['admin.logo-mini' => $this->getLogoMini()]);
        config(['admin.title' => $this->getTitle()]);
        config(['admin.uuid' => $this->getUuid()]);
    }

    public function getHost()
    {
        return $this->app->request->headers->get('host') ;
    }

    public function getTenantNameByHost()
    {
        $this->host = explode('.' , $this->getHost());
        // 如果没有二级域名，跳转到公共域名入口
        if(count($this->host) <= 2){
            abort(500) ; // 这里直接抛出500，应用应该跑在二级域名里面。
        }
        $subDomain = $this->host [0];

        $tenant = null;

        // 过滤prefix
        $prefix = config('winged.tenants.prefix') ;

        if(is_string($prefix))
        {
            $subDomain = str_replace( $prefix , "" , $subDomain) ;
        }
        // 检查是否是公共域名
        $common_domain = config('winged.tenants.common_domain') ;

        if($common_domain == $subDomain)
        {
            $this->isCommonDomain = true ;
            return config('winged.tenants.default_tenant');
        }

        return $subDomain ;
    }

    public function getDisplayName()
    {
        return $this->admin_display_name;
    }

    public function getDisplayShortName()
    {
        return $this->admin_display_short_name;
    }

    public function getLogo()
    {
        return sprintf("<img src='%s' />" , $this->logo);
    }

    public function getLogoMini()
    {
        return sprintf("<img src='%s' />" , $this->logo_mini);
    }

    public function getLogoLarge()
    {
        return $this->logo_large;
    }

    public function getBackground()
    {
        return $this->admin_background;
    }

    public function getStyle()
    {
        return $this->style;
    }

    public function getTitle()
    {
        return $this->getDisplayName().'管理后台';
    }

    public function getMainTitle()
    {
        return $this->admin_title;
    }

    public function getUuid()
    {
        return $this->tenant_uuid ;
    }

    public function getService()
    {
        return $this->services;
    }

    public function getRedirect()
    {
        return $this->admin_default_url ;
    }

    public function getBarcodes()
    {
        return $this->barcodes;
    }

    public function isCommonDomain()
    {
        return $this->isCommonDomain ;
    }

    public function getPushConfig()
    {
        return [
            'android' => [
                'appid' => $this->android_push_appid,
                'appkey' => $this->android_push_appkey,
                'secret' => $this->android_push_secret,
            ],
            'ios' => [
                'appid' => $this->ios_push_appid,
                'appkey' => $this->ios_push_appkey,
                'secret' => $this->ios_push_secret,
            ],
        ];
    }

    public function getDeaprtmentDomain()
    {
        try {
            return json_decode($this->department_domain,true);
        }
        catch (\Exception $exception){
            return $this->department_domain ;
        }
    }

    public function loginRedirect($tenant_id = null)
    {
        $schme = config('winged.backend.scheme');
        // 公共入口进入 .
        if($this->isCommonDomain()){
            $config =  $this->getConfig($tenant_id);
            $redirect = "/" . ltrim($config ['admin_default_url'], "/");
            $prefix = config('winged.tenants.prefix') ;
            $url = "{$schme}://{$prefix}{$tenant_id}.{$this->host[1]}.{$this->host[2]}{$redirect}";
        }else{
            // 从他自己的入口进入
            $url = $this->getRedirect();
        }
        return $url ;
    }

    public function hasServe($service)
    {
        $service = explode(',',$service);
        $services = $this->getService();
        if(count($service) == 1){
            return in_array($service[0] ,$services);
        }else{
            foreach ($service as $serve){
                if(in_array($serve ,$services)){
                    return true ;
                }
            }
            return false;
        }
    }

}
